package net.xxcl.stuManager.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.xxcl.stuManager.mapper.ClassMapper;
import net.xxcl.stuManager.pojo.Student;
import net.xxcl.stuManager.pojo.Teacher;

public class ClassAction extends HttpServlet {

	ClassMapper classMapper = new ClassMapper();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		
		if(request.getParameter("action").equalsIgnoreCase("selectByTeacherid")){
			selectByTeacherid(request, response);
		} else if (request.getParameter("action").equalsIgnoreCase("add")){
			
		}
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}
	
	/**
	 * 根据教师id查找所管理的班级
	 * @throws IOException 
	 */
	private void selectByTeacherid(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		Teacher teacher = (Teacher)request.getSession().getAttribute("teacher");
		List<Teacher> list = classMapper.selectByTeacherid(teacher.getId());
		JSONArray json = JSONArray.fromObject(list);
		out.print(json);
		
		out.flush();
		out.close();
	}
}
